<?php
// 引入配置管理
$config = require '../../config.php';
$sql_server_config = $config['databases']['sql_server'] ?? [];
?>

<div class="query-panel">
    <div class="query-filters">
        <h4>SQL Server</h4>
        
        <!-- 启用/禁用开关 - 独立一行 -->
        <div style="margin-bottom: 20px; padding: 15px; background-color: #f8f9fa; border-radius: 4px; border-left: 4px solid #007bff;">
            <div class="filter-container" style="justify-content: flex-start;">
                <label style="font-weight: 600; color: #333; margin-right: 15px;">服务状态:</label>
                <label class="switch" style="margin-right: 15px;">
                    <input type="checkbox" id="sqlserver-enabled" <?php echo ($sql_server_config['enabled'] ?? false) ? 'checked' : ''; ?> onchange="toggleSqlServerService()">
                    <span class="slider"></span>
                </label>
                <span id="sqlserver-status-text" class="status-text">
                    <?php echo ($sql_server_config['enabled'] ?? false) ? '已启用' : '已禁用'; ?>
                </span>
            </div>
        </div>
        
        <div id="sqlserver-config-form" class="config-form-section" <?php echo ($sql_server_config['enabled'] ?? false) ? '' : 'style="opacity: 0.6; pointer-events: none;"'; ?>>
            <div style="margin-bottom: 15px;">
                <div class="filter-container">
                    <label>SecretId:</label>
                    <input type="text" id="sqlserver-secret-id" class="form-input" placeholder="腾讯云API密钥ID，格式: AKID********" value="<?php echo htmlspecialchars($sql_server_config['secret_id'] ?? ''); ?>" style="flex: 1;" <?php echo ($sql_server_config['enabled'] ?? false) ? '' : 'disabled'; ?>>
                    
                    <label>SecretKey:</label>
                    <input type="password" id="sqlserver-secret-key" class="form-input" placeholder="腾讯云API密钥Key，40位字符" value="<?php echo htmlspecialchars($sql_server_config['secret_key'] ?? ''); ?>" style="flex: 1;" <?php echo ($sql_server_config['enabled'] ?? false) ? '' : 'disabled'; ?>>
                    
                    <label>地域:</label>
                    <select id="sqlserver-region" class="form-input" style="width: 140px;" <?php echo ($sql_server_config['enabled'] ?? false) ? '' : 'disabled'; ?>>
                        <!-- 中国大陆 -->
                        <option value="ap-guangzhou" <?php echo ($sql_server_config['region'] ?? '') === 'ap-guangzhou' ? 'selected' : ''; ?>>华南地区(广州)</option>
                        <option value="ap-shanghai" <?php echo ($sql_server_config['region'] ?? '') === 'ap-shanghai' ? 'selected' : ''; ?>>华东地区(上海)</option>
                        <option value="ap-beijing" <?php echo ($sql_server_config['region'] ?? '') === 'ap-beijing' ? 'selected' : ''; ?>>华北地区(北京)</option>
                        <option value="ap-chengdu" <?php echo ($sql_server_config['region'] ?? '') === 'ap-chengdu' ? 'selected' : ''; ?>>西南地区(成都)</option>
                        <option value="ap-chongqing" <?php echo ($sql_server_config['region'] ?? '') === 'ap-chongqing' ? 'selected' : ''; ?>>西南地区(重庆)</option>
                        <option value="ap-nanjing" <?php echo ($sql_server_config['region'] ?? '') === 'ap-nanjing' ? 'selected' : ''; ?>>华东地区(南京)</option>
                        <option value="ap-shenzhen" <?php echo ($sql_server_config['region'] ?? '') === 'ap-shenzhen' ? 'selected' : ''; ?>>华南地区(深圳)</option>
                        <!-- 中国特别行政区 -->
                        <option value="ap-hongkong" <?php echo ($sql_server_config['region'] ?? '') === 'ap-hongkong' ? 'selected' : ''; ?>>中国香港</option>
                        <!-- 亚太地区 -->
                        <option value="ap-singapore" <?php echo ($sql_server_config['region'] ?? '') === 'ap-singapore' ? 'selected' : ''; ?>>亚太东南(新加坡)</option>
                        <option value="ap-bangkok" <?php echo ($sql_server_config['region'] ?? '') === 'ap-bangkok' ? 'selected' : ''; ?>>亚太东南(曼谷)</option>
                        <option value="ap-jakarta" <?php echo ($sql_server_config['region'] ?? '') === 'ap-jakarta' ? 'selected' : ''; ?>>亚太东南(雅加达)</option>
                        <option value="ap-tokyo" <?php echo ($sql_server_config['region'] ?? '') === 'ap-tokyo' ? 'selected' : ''; ?>>亚太东北(东京)</option>
                        <option value="ap-seoul" <?php echo ($sql_server_config['region'] ?? '') === 'ap-seoul' ? 'selected' : ''; ?>>亚太东北(首尔)</option>
                        <option value="ap-mumbai" <?php echo ($sql_server_config['region'] ?? '') === 'ap-mumbai' ? 'selected' : ''; ?>>亚太南部(孟买)</option>
                        <!-- 北美地区 -->
                        <option value="na-siliconvalley" <?php echo ($sql_server_config['region'] ?? '') === 'na-siliconvalley' ? 'selected' : ''; ?>>美国西部(硅谷)</option>
                        <option value="na-ashburn" <?php echo ($sql_server_config['region'] ?? '') === 'na-ashburn' ? 'selected' : ''; ?>>美国东部(弗吉尼亚)</option>
                        <option value="na-toronto" <?php echo ($sql_server_config['region'] ?? '') === 'na-toronto' ? 'selected' : ''; ?>>北美地区(多伦多)</option>
                        <!-- 欧洲地区 -->
                        <option value="eu-frankfurt" <?php echo ($sql_server_config['region'] ?? '') === 'eu-frankfurt' ? 'selected' : ''; ?>>欧洲地区(法兰克福)</option>
                        <option value="eu-moscow" <?php echo ($sql_server_config['region'] ?? '') === 'eu-moscow' ? 'selected' : ''; ?>>欧洲东北(莫斯科)</option>
                        <!-- 南美地区 -->
                        <option value="sa-saopaulo" <?php echo ($sql_server_config['region'] ?? '') === 'sa-saopaulo' ? 'selected' : ''; ?>>南美地区(圣保罗)</option>
                    </select>
                    
                    <label>实例ID:</label>
                    <input type="text" id="sqlserver-instance-id" class="form-input" placeholder="SQL Server实例ID，格式: mssql-xxxxxxxx" value="<?php echo htmlspecialchars($sql_server_config['instance_id'] ?? ''); ?>" style="flex: 1;" <?php echo ($sql_server_config['enabled'] ?? false) ? '' : 'disabled'; ?>>
                </div>
            </div>
            
            <div style="margin-bottom: 15px;">
                <div class="filter-container">
                    <label>连接地址:</label>
                    <input type="text" id="sqlserver-host" class="form-input" placeholder="连接地址，如：mssql-xxx.sql.tencentcdb.com" value="<?php echo htmlspecialchars($sql_server_config['host'] ?? ''); ?>" style="flex: 1;" <?php echo ($sql_server_config['enabled'] ?? false) ? '' : 'disabled'; ?>>
                    
                    <label>端口:</label>
                    <input type="text" id="sqlserver-port" class="form-input" placeholder="1433" value="<?php echo htmlspecialchars($sql_server_config['port'] ?? '1433'); ?>" style="width: 80px;" <?php echo ($sql_server_config['enabled'] ?? false) ? '' : 'disabled'; ?>>
                    
                    <label>库名:</label>
                    <input type="text" id="sqlserver-database" class="form-input" placeholder="数据库名称" value="<?php echo htmlspecialchars($sql_server_config['database'] ?? ''); ?>" style="flex: 1;" <?php echo ($sql_server_config['enabled'] ?? false) ? '' : 'disabled'; ?>>
                    
                    <label>用户名:</label>
                    <input type="text" id="sqlserver-username" class="form-input" placeholder="数据库用户名" value="<?php echo htmlspecialchars($sql_server_config['username'] ?? ''); ?>" style="flex: 1;" <?php echo ($sql_server_config['enabled'] ?? false) ? '' : 'disabled'; ?>>
                    
                    <label>密码:</label>
                    <input type="password" id="sqlserver-password" class="form-input" placeholder="数据库密码" value="<?php echo htmlspecialchars($sql_server_config['password'] ?? ''); ?>" style="flex: 1;" <?php echo ($sql_server_config['enabled'] ?? false) ? '' : 'disabled'; ?>>
                </div>
            </div>
            
            <div class="query-controls">
                <button class="btn btn-primary" onclick="window.testSqlServerConnection()" style="margin-right: 10px;" <?php echo ($sql_server_config['enabled'] ?? false) ? '' : 'disabled'; ?>>测试连接</button>
                <button class="btn btn-secondary" onclick="window.saveSqlServerConfig()" style="margin-right: 10px;" <?php echo ($sql_server_config['enabled'] ?? false) ? '' : 'disabled'; ?>>保存配置</button>
                <button class="btn btn-success" onclick="window.uploadSqlServerSql()" <?php echo ($sql_server_config['enabled'] ?? false) ? '' : 'disabled'; ?>>上传SQL</button>
                <input type="file" id="sqlserver-sql-file" accept=".sql" style="display:none;" onchange="window.handleSqlServerSqlFile(this)">
            </div>
        </div>
    </div>
</div>
